package com.syf.starter.soa;

import com.syf.core.engine.SyFrameContext;
import com.syf.core.server.SyServer;
import com.syf.kit.SyfJsonMapper;
import com.syf.kit.soa.SOARegionDocument;
import com.syf.starter.config.SySOAServerConfiguration;
import com.syf.starter.soa.protocol.RpcTcpServer;

import java.util.concurrent.CountDownLatch;

public class SySOAServer implements SyServer {
    private final SySOAServerConfiguration configuration;

    private final SyFrameContext syFrameContext;

    private final SyfJsonMapper jsonMapper;

    private final SOARegionDocument soaRegionDoc;

    private RpcTcpServer tcpServer;

    public SySOAServer(SySOAServerConfiguration configuration, SyFrameContext syFrameContext, SOARegionDocument soaRegionDoc) {
        this.configuration = configuration;
        this.syFrameContext = syFrameContext;
        this.soaRegionDoc = soaRegionDoc;
        this.jsonMapper = new SyfJsonMapper(true, true);
        initialize();
    }

    private void initialize() {
        this.tcpServer = new RpcTcpServer(configuration, soaRegionDoc);
    }

    @Override
    public void startup(CountDownLatch latch) {
        tcpServer.startup(syFrameContext.getServiceInvoker(), jsonMapper, configuration.getServiceRequestTimeout(), latch);
    }

    @Override
    public void gracefullyShutdown() {

    }
}
